<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>员工管理</el-breadcrumb-item>
      <el-breadcrumb-item>员工管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button type="primary" class="btn" @click="showadd()"
        >添加员工</el-button
      >
      <el-row>
        <el-table
          :data="
            users.slice((currentPage - 1) * pagesize, currentPage * pagesize)
          "
          border
          style="width: 100%"
        >
          <el-table-column label="账号">
            <template slot-scope="scope">
              <span>{{ scope.row.username }}</span>
            </template>
          </el-table-column>
          <el-table-column label="密码">
            <template slot-scope="scope">
              <span>{{ scope.row.password }}</span>
            </template>
          </el-table-column>
          <el-table-column label="员工姓名">
            <template slot-scope="scope">
              <span>{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="所属部门">
            <template slot-scope="scope">
              <span>{{ scope.row.department }}</span>
            </template>
          </el-table-column>
          <el-table-column label="性别">
            <template slot-scope="scope">
              <span text-align="center">{{ scope.row.sex }}</span>
            </template>
          </el-table-column>
          <el-table-column label="电话">
            <template slot-scope="scope">
              <span text-align="center">{{ scope.row.phone }}</span>
            </template>
          </el-table-column>
          <el-table-column label="年龄">
            <template slot-scope="scope">
              <span text-align="center">{{ scope.row.age }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="show(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="remove(scope.row.username)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="40"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="修改用户信息"
      :visible.sync="editdialogVisible"
      width="50%"
      :before-close="handleClose"
      @close="closed"
    >
      <el-form
        :v-model="addForm"
        :rules="addrules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="账号" prop="username">
          <el-input disabled v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="员工名称" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="所属部门" prop="department">
          <el-select v-model="nowopt" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="addForm.sex"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="addForm.age"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="change">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="添加员工"
      :visible.sync="adddialogVisible"
      width="50%"
      :before-close="handleClose"
      @close="closed"
    >
      <el-form
        :v-model="addForm1"
        :rules="addrules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model="addForm1.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm1.password"></el-input>
        </el-form-item>
        <el-form-item label="员工姓名" prop="name">
          <el-input v-model="addForm1.name"></el-input>
        </el-form-item>
        <el-form-item label="所属部门" prop="department">
          <el-select v-model.number="nowopt" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="addForm1.sex"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addForm1.phone"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="addForm1.age"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addstaff1">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getAll, upsert, removed } from "../../network/EmpManage.js";
import axios from "axios";
export default {
  data() {
    return {
      users: [],
      query: "",
      currentPage: 1,
      pagesize: 5,
      adddialogVisible: false,
      editdialogVisible: false,
      state: "正常",
      nowopt: null,
      options: [
        {
          value: 0,
          label: "管理部",
        },
        {
          value: 1,
          label: "销售部",
        },
        {
          value: 2,
          label: "采购部",
        },
        {
          value: 3,
          label: "仓储部",
        },
        {
          value: 4,
          label: "人事部",
        },
      ],
      dep_role: [
        {
          role: "ROLE_ADMIN",
          department: "管理部",
        },
        {
          role: "ROLE_SELL",
          department: "销售部",
        },
        {
          role: "ROLE_PURCHASE",
          department: "采购部",
        },
        {
          role: "ROLE_INVENTORY",
          department: "仓储部",
        },
        {
          role: "ROLE_STAFF",
          department: "人事部",
        },
      ],
      addForm: {
        username: "",
        name: "",
        department: "",
        age: "",
        sex: "",
        phone: "",
        password: "",
        role: "",
      },

      addForm1: {
        username: "",
        name: "",
        department: "",
        age: "",
        sex: "",
        phone: "",
        password: "",
        role: "",
      },
      addrules: {
        // role: [
        //   { required: true, message: "请输入role", trigger: "blur" },
        // ],
        // username: [
        //   { required: true, message: "请输入用户名", trigger: "blur" },
        // ],
        // password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        // department: [
        //   { required: true, message: "请输入所属部门", trigger: "blur" },
        // ],
        // sex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        // phone: [{ required: true, message: "请输入电话", trigger: "blur" }],
        // age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
      },
    };
  },
  created() {
    getAll()
      .then((res) => {
        var result = res.data.obj;
        this.users = result;
        console.log(res.data.obj);
      })
      .catch(function (error) {
        console.log(error);
      });
  },
  methods: {
    showadd(){
      this.adddialogVisible=true;
      this.nowopt=null;
    },
    change() {
      this.addForm.department = this.dep_role[this.nowopt].department;
      this.addForm.role = this.dep_role[this.nowopt].role;
      console.log(this.addForm);
      upsert(this.addForm)
        .then((res) => {
          console.log(res);
          if (res.data.code == "200") {
            this.$message({
              message: res.data.status,
              type: "success",
            });
            getAll()
              .then((res) => {
                var result = res.data.obj;
                this.users = result;
                console.log(res.data.obj);
              })
              .catch(function (error) {
                console.log(error);
              });
            this.editdialogVisible = false;
          } else {
            this.$message(res.data.status);
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    getlist() {},
    handleSizeChange(val) {
      this.pagesize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    closed() {
      this.$refs.addFormRef.resetFields();
    },
    show(item) {
      this.editdialogVisible = true;
      //  console.log(id);
      this.addForm = JSON.parse(JSON.stringify(item));
      this.addForm.password=''
      this.nowopt=null;
    },
    remove(username) {
      removed({ username: username })
        .then((res) => {
          if (res.data.code == "200") {
            this.$message({
              message: res.data.status,
              type: "success",
            });
            getAll()
              .then((res) => {
                var result = res.data.obj;
                this.users = result;
                console.log(res.data.obj);
              })
              .catch(function (error) {
                console.log(error);
              });
          } else {
            this.$message(res.data.status);
          }
        })
        .catch((error) => {
          console.log(error); //异常
        });
    },
    addstaff1() {
      this.addForm1.department = this.dep_role[this.nowopt].department;
      this.addForm1.role = this.dep_role[this.nowopt].role;
      console.log(this.addForm1);
      upsert(this.addForm1)
        .then((res) => {
          console.log(res);
          if (res.data.code == "200") {
            this.$message({
              message: res.data.status,
              type: "success",
            });
            getAll()
              .then((res) => {
                var result = res.data.obj;
                this.users = result;
                console.log(res.data.obj);
              })
              .catch(function (error) {
                console.log(error);
              });
            this.adddialogVisible = false;
          } else {
            this.$message(res.data.status);
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    },
  },
};
</script>


<style>
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 12px;
}
.btn {
  margin-left: 880px;
}
.el-table {
  margin-top: 15px;
}
.el-pagination {
  text-align: center;
}
</style>
